type=$1
action=$2
node=$3
JAVA_HOME=""
ES_HOME=/root/soft/elasticsearch_$node
KIBANA_HOME=/home/xieqihui/soft/kibana
MB_HOME=/home/xieqihui/soft/metricbeat
es_password=elastic

#启动es
es_start() {
    etc=$(cat /etc/security/limits.conf | grep "^es$unlimited")

    if [ ! "$etc" ]; then
        echo "es soft memlock unlimited" >>/etc/security/limits.conf
        echo "es hard memlock unlimited" >>/etc/security/limits.conf
    fi
    sysctl -w vm.max_map_count=262144
    su - es -c "${ES_HOME}/bin/elasticsearch -p pid -d"
}

#停止es
es_stop() {
    if [ -f "${ES_HOME}/pid" ]; then
        pid=$(cat ${ES_HOME}/pid)
        rm -f ${ES_HOME}/pid
        kill $pid
    else
        pid=$(su - es -c "ps -ef" | grep elasticsearch | grep "\-p pid \-d" | awk '{print $2}')
        kill $pid
    fi
}

#查看es状态
es_status() {
    if [ -f "${ES_HOME}/pid" ]; then
        pid=$(cat ${ES_HOME}/pid)
        echo "$node es 正在运行, pid：$pid"
        return 0
    fi
    echo "$node es 未运行"
}

# 设置密码
set_password() {
    expect -v
    if [ ! $? = 0 ]; then
        yum install expect -y
    fi
    expect <<EOF
    spawn ./bin/elasticsearch-setup-passwords interactive
    expect {
        "*y/N*" { send "y\r"; exp_continue }
        "Enter password*" { send "$es_password\r"; exp_continue }
        "Reenter password*" { send "$es_password\r"; exp_continue }
    }
EOF

}
#es操作函数
es() {
    cd ${ES_HOME}
    action=$1
    case $action in
    start)
        es_start
        ;;
    stop)
        es_stop
        ;;
    status)
        es_status
        ;;
    restart)
        es_stop
        es_start
        ;;
    set)
        set_password
        ;;
    esac

}

kibana_stop() {
    cd ${KIBANA_HOME}
    if [ -f "./pid" ]; then
        pid=$(cat ./pid)
        kill $pid
    fi
}

#kibana操作函数
kibana() {
    cd ${KIBANA_HOME}
    action=$1

    case $action in
    start)
        echo "${KIBANA_HOME}"
        nohup ./bin/kibana --allow-root >./log/kibana.log 2>&1 &
        echo
        ;;

    stop)
        kibana_stop
        ;;
    status)
        if [ -f "./pid" ]; then
            pid=$(cat ./pid)
            echo "kibana 正在运行, pid: $pid"
            return 0
        fi
        echo "kibana 未运行"
        ;;

    restart)
        kibana_stop
        nohup ./bin/kibana >./log/kibana.log 2>&1 &
        echo
        ;;

    esac
}

#启动metricbeat
mb_start() {
    nohup ./metricbeat -c metricbeat.yml >/dev/null 2>&1 &
    echo $! >pid
}
#查看metricbeat状态
mb_status() {
    if [ -f "./pid" ]; then
        pid=$(cat ./pid)
        echo "metricbeat 正在运行, pid: $pid"
        return 0
    fi
    echo "metricbeat 未运行"
}

#停止metricbeat
mb_stop() {
    pid=$(cat ./pid)
    kill $pid
    rm ./pid
}

#metricbeat操作函数
metricbeat() {
    cd ${MB_HOME}
    action=$1
    case $action in
    start)
        mb_start
        ;;
    stop)
        mb_stop
        ;;
    status)
        mb_status
        ;;

    restart)
        mb_stop
        mb_start
        ;;

    esac
}

#入口
case $type in
es)
    es $action
    ;;
kibana)
    kibana $action
    ;;
mb)
    metricbeat $action
    ;;
esac
